package com.kool.devops.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.exception.AppException;
import com.kool.devops.service.DevService;
import com.kool.devops.service.DevToolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @description: excel生成sql
 * @author: tianhe
 * @create: 2018/11/10 21:06
 */
@Controller
public class GenSqlAction{
    @Autowired
    private DevService devService;

    @ResponseBody
    @RequestMapping(value = "/devops/dev/genSql", method = RequestMethod.POST)
    public JSONObject doAction(@RequestParam("file") MultipartFile file) throws AppException {
        String sql =  devService.genSql(file);

        JSONObject joProp = new JSONObject();
        joProp.put("sql", sql);

        JSONArray jaSec = new JSONArray();
        jaSec.add(joProp);

        JSONObject joOut = new JSONObject();
        joOut.put("devops_gen_sql", jaSec);

        return joOut;
    }


    /**
     * @DESCRIBE
     * @DATE 2018年1月13日 下午10:23:36
     *
     * @param uploadFile
     * @return
     * @throws AppException
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @ResponseBody
    @RequestMapping(value = "/sys/devtools/genSql.json", method = RequestMethod.POST)
    public JSONObject doActionB(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request,
                               HttpServletResponse response) throws AppException {
        JSONObject pkgOut = new JSONObject();
        JSONArray sectionOut = new JSONArray();
        JSONObject propOut = new JSONObject();
        DevToolsService service = new DevToolsService();
        String sql = service.excelToSql(uploadFile);

        propOut.put("sql", sql);
        sectionOut.add(propOut);
        pkgOut.put("SYXLSSQLZ", sectionOut);
        return pkgOut;
    }
}
